home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer Conversion
- Date: 20 Jan 1996 22:46 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <20JAN199622465412@erich.triumf.ca>
- References: <4ds4jq$fo4@su3.in.net>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4ds4jq$fo4@su3.in.net>, poundss@in.net (Sam Pounds) writes...
- >I am having a problem with a "string concatenate" function.
- >When I compile my little program it works, but I get a
- >"suspicious pointer" conversion warning. The function is
- >below and I call it with two strings that I want to concatenate.
- >
- >char *my_strcat(const char *a, const char *b)
- >{
- > char done[1024];
- > char *p = done;
- >
- > while (*a)
- > *p++ = *a++;
- > while (*b)
- > *p++ = *b++;
- > *p = '\0';
- > return done; /* this is the suspicious pointer conversion error */
- >}
-
- Your function is declared to return a char pointer, and you are returning an
- array, which is not the same thing. Sometimes char array names act like char
- pointers (particularly when used as function parameters), but not here.
-
- A more important problem here is that "done" is an automatic array which will
- cease to exist when the function returns, possibly being over-written on the
- next call to another function.
-
- You could declare "done" as a char pointer, and malloc() some memory for it to
- point to - this would solve both problems, but the calling function would have
- to free() that malloc()ed memory sometime.
-
- You could also declare done as a static array (just add "static" before the
- present declaration), then the array will exist for the life of the program.
- I _think_ (and I know someone will correct me if I'm wrong) that changeing
- "return done;" to "return &done;" will fix the "suspicious pointer conversion"
- error.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-
-
-